package org.dtrd.modules.patient.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.modules.patient.service.IDtrdEntRdReportService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 月报任务
 * @author qr
 * @date 2022/1/22 17:32
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class ReportMonthJob implements Job {

    @Autowired
    private IDtrdEntRdReportService reportService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<Integer> patientIds = reportService.getValidPatientIds();
        if (CollectionUtil.isNotEmpty(patientIds)) {
            // 生成月报
            for (Integer patientId : patientIds) {
                DateTime now = DateTime.now();
                // 设置报告名字
                String reportName = String.format("糖尿病逆转%d年%d月报告",
                        now.getField(DateField.YEAR),
                        now.getField(DateField.MONTH) + 1);
                reportService.createReportMonth(patientId, reportName);
                log.info("----------患者: {} 生成月报: {}----------", patientId, reportName);
            }
        }
    }
}
